#!/bin/sh /etc/rc.common
START=95

# Convert zip to tar.gz
zip2tar() {
	unzip_install=`which unzip`
	[ -n "$unzip_install" ] || {
		opkg update
		opkg install unzip
	}
	unzip_install=`which unzip`
	[ -n "$unzip_install" ] || {
		echo "Could not found unzip."
		exit 0
	}
	[ -n "$1" ] || {
		echo "Require a path. ( i.e. /etc/init.d/thunder start zip2tar /mnt/sda1 )"
		exit 0
	}
	[ -d "$1" ] || {
		echo "$1 do not exist."
		exit 0
	}
	cur_path="$(cd "$(dirname "$0")"; pwd)"
	[ -n "$1" ] && cur_path="$1"
	ls "$cur_path"| while read LINE;do
		[ -d "$cur_path/$LINE" ] && {
			empty=`ls -A "$cur_path/$LINE/"*".zip"`
			[ -n "$empty" ] && {
				ls "$cur_path/$LINE/"*".zip"| while read FILENAME;do
					[ -f "$FILENAME" ] && {
						folder="${FILENAME%.*}"
						file="${folder##*/}"
						[ -f "$cur_path/$LINE/$file.tar.gz" ] || {
							unzip "$FILENAME" -d "${folder}"
							cd "$folder"
							tar -czf "$cur_path/$LINE/$file.tar.gz" *
							rm -rf "$folder"
							# rm -f "$FILENAME"
						}
					}
				done
			}
		}
	done
	cd "$cur_path"
	exit 0
}

# Update Xware
update() {
	if [ -n "$file" ] && [ -n "$url" ] && [ -n "$model" ];then
		[ -f "$file/Thunder/portal" ] && "$file/Thunder/portal" -s
		[ -d "$file/Thunder" ] || mkdir "$file/Thunder"
		[ -d "$file/Thunder/lib" ] || mkdir "$file/Thunder/lib"
		version=`cat "$file/Thunder"/version`
		version_lastest="`wget -qO- $url 2>/dev/null|grep -o '[0-9]\\+\\.[0-9]\\+\\.[0-9]\\+'|awk 'END{print}'`"
		[ "$version" = "$version_lastest" ] || {
			wget -O "$file/Thunder/Xware${version_lastest}_${model}.tar.gz" "$url/$version_lastest/Xware${version_lastest}_${model}.tar.gz"
			[ -f "$file/Thunder/Xware${version_lastest}_${model}.tar.gz" ] && {
				rm -f "$file/Thunder/portal"
				rm -f "$file/Thunder/lib/"*
				tar -xzf "$file/Thunder/Xware${version_lastest}_${model}.tar.gz" -C "$file/Thunder/lib"
				cp "$file/Thunder/lib/portal" "$file/Thunder/portal"
				chmod +x "$file/Thunder/portal"
				chmod +x "$file/Thunder/lib/"*
				echo "$version_lastest" >"$file/Thunder/version"
				rm -f "$file/Thunder/lib/portal"
				[ "$vod" = 1 ] && rm -f "$file/Thunder/lib/vod_httpserver"
				rm -f "$file/Thunder/Xware${version_lastest}_${model}.tar.gz"
			}
		}
	else
		echo "Missed Xware path, update url or router model."
	fi
}

start() {
	local enable device file vod model url
	config_load "thunder"
	config_get enable config enable 0
	config_get device config device
	config_get file config file
	config_get vod config vod 1
	config_get model config model
	config_get url config url

	[ "$1" = 'update' ] && update

	[ "$1" = 'zip2tar' ] && zip2tar "$2"

	rm -f /tmp/etc/thunder_mounts.cfg
	rm -f /tmp/etc/etm.cfg
	rm -f /tmp/etc/download.cfg

	path1="$file/Thunder/cfg/thunder_mounts.cfg"
	path2="$file/Thunder/cfg/etm.cfg"
	path3="$file/Thunder/cfg/download.cfg"
	
	[ "$vod" = "1" ] && {
		rm -f "$file/Thunder/lib/vod_httpserver"
		rm -f "$file/Thunder/vod_httpserver"
	}

	[ "$enable" -eq 0 ] &&	exit 0

	ln -s "$path1" "/tmp/etc/thunder_mounts.cfg"
	ln -s "$path2" "/tmp/etc/etm.cfg"
	ln -s "$path3" "/tmp/etc/download.cfg"

	"$file/Thunder/portal"
	sleep 3
}

stop() {
	file=$(uci get thunder.config.file)
	$file/Thunder/portal -s
}

restart() {
	stop
	sleep 1
	start
}
